﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Cqrs.Common;

namespace Cqrs.StateMachineDesigner.CustomCode
{
    /// <summary>
    /// Interaction logic for AddHocMethods.xaml
    /// </summary>
    public partial class AdHocMethods : Window
    {
        private List<DomainProperty> _domainProperties;
        public List<CommandEvent> AdhocMethods {get; set;}
        private List<CommandEvent> _orginalAdhocMethods;

        public AdHocMethods()
        {
            InitializeComponent();
        }

        public List<CommandEvent> ShowForm(List<CommandEvent> adhocMethods, List<DomainProperty> domainProperties)
        {            
            _domainProperties = domainProperties;
            this.AdhocMethods = adhocMethods;
            _orginalAdhocMethods = new List<CommandEvent>(this.AdhocMethods);
            dgMethods.DataContext = this.AdhocMethods;

            this.ShowDialog();
            return AdhocMethods;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            AddCommandEvent addCommandEvent = new AddCommandEvent();
            try
            {
                CommandEvent commandEvent = addCommandEvent.NewCommandEvent();
                if (addCommandEvent.Accepted)
                {
                    AdhocMethods.Add(commandEvent);
                    dgMethods.Items.Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                addCommandEvent = null;
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.AdhocMethods = _orginalAdhocMethods;
                this.Close();
            }
            catch { }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch { }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            EditSelectedRow();
        }

        private void EditSelectedRow()
        {
            AddCommandEvent editCommandEvent = new AddCommandEvent();
            try
            {
                if (dgMethods.SelectedItems.Count == 0)
                    return;

                CommandEvent commandEvent = ((CommandEvent)dgMethods.SelectedItems[0]);
                editCommandEvent.txtEvent.Text = commandEvent.Event;
                editCommandEvent.txtCommand.Text = commandEvent.Command;

                commandEvent = editCommandEvent.EditCommandEvent(commandEvent);
                if (editCommandEvent.Accepted)
                {
                    dgMethods.Items.Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dgMethods.SelectedItems.Count == 0)
                    return;

                AdhocMethods.Remove((CommandEvent)dgMethods.SelectedItems[0]);
                dgMethods.Items.Refresh();
            }
            catch { }
        }

        private void dgMethods_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            EditSelectedRow();
        }
    }
}
